<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/9
 * Time: 上午1:49
 */

namespace Home\Model;
use Think\Model;


class UserExtModel extends Model{

    protected $tableName = 'user_ext';
    protected $pk         = 'user_ext_id';

    public function getYInfo($jobs,$duration){

        //根据查询条件在user_ext表中获取符合条件用户user_id及基本信息
        $user_ext_field = array('user_id','degree_year','degree_school','degree_major','level_y','jobs');
        $user_ext_order = '`level_y` desc,`update_time` desc';
        $job_arr = explode(",",$jobs);
        for($i=0;$i < count($job_arr);$i++){
            $job_arr[$i] = '%'.$job_arr[$i].'%';
        }
        $duration_arr = explode(",",$duration);
        for($i=0;$i < count($duration_arr);$i++){
            $duration_arr[$i] = '%'.$duration_arr[$i].'%';
        }
        $user_ext_map['jobs'] = array('like',$job_arr,'or');
        $user_ext_map['duration'] = array('like',$duration_arr,'or');
        //dump($user_ext_map);
        $user_ext_result = $this->table('User_ext')->field($user_ext_field)->order($user_ext_order)->where($user_ext_map)->select();
        //dump($user_ext_result);
        if(is_array($user_ext_result)){
            foreach($user_ext_result as &$user_ext_result_one){
                //根据user_id在user表中获取status=1的user_name
                $user_field = array('user_name','status');
                $user_map['user_id'] = array('eq',$user_ext_result_one['user_id']);
                $user_map['status'] = array('eq',1);
                $user_info = $this->table('User')->field($user_field)->where($user_map)->select();
                if(is_array($user_info)){
                    $user_ext_result_one['user_name'] = $user_info[0]['user_name'];
                    $user_ext_result_one['status'] = $user_info[0]['status'];
                }else{
                    //user表中获取信息失败
                }
                //根据user_id在resume表中获取resume_id
                $resume_field = array('resume_id');
                $resume_map['user_id'] = array('eq',$user_ext_result_one['user_id']);
                $resume_info = $this->table('Resume')->where($resume_map)->field($resume_field)->select();
                if(is_array($resume_info)){
                    $user_ext_result_one['resume_id'] = $resume_info[0]['resume_id'];
                }else{
                    //resume表获取信息失败
                }
                //根据resume_id在experience表中获取project和role
                $experience_field = array('project_name','role');
                $experience_order = '`update_time` desc';
                $experience_map['resume_id'] = array('eq',$user_ext_result_one['resume_id']);
                $experience_info = $this->table('Experience')->field($experience_field)->order($experience_order)->where($experience_map)->select();
                if(is_array($experience_info)){
                    $user_ext_result_one['experiences'] = $experience_info;
                }else{
                    //experience表获取信息失败
                }
            }
            //排除status不等于1的用户
            //dump($user_ext_result);
            return $user_ext_result;
        }else{
            // user_ext表获取信息失败
            return 0;
        }

    }

    //修改手机是否公布状态
    public function changePhoneStatus($user_id){
        $ispublic_phone_arr = $this->field(array('ispublic_phone'))->where(array('user_id'=>$user_id))->select();
        $ispublic_phone = $ispublic_phone_arr[0];
        if(1 == $ispublic_phone){
            $data['ispublic_phone'] = 3;
        }else if(2 == $ispublic_phone){
            $data['ispublic_phone'] = 4;
        }else{
            return false;
        }
        $user_ext_result = $this->save($data);
        if($user_ext_result){
            return true;
        }else{
            return false;
        }

    }

    //获取乙方基本信息扩展信息
    public function getFetcherBaseInfo($user_id){
        $user_ext_field = array('user_id','degree_year','degree_school','degree_major','ispublic_degree','ispublic_birthday','ispublic_phone','level_j','level_y');
        $user_ext_map['user_id'] = array('eq',$user_id);
        $user_ext_result = $this->field($user_ext_field)->where($user_ext_map)->select();
        if(is_array($user_ext_result)){
            if($user_ext_result[0]['ispublic_degree'] != 1){
                $user_ext_result[0]['degree_degree'] = '';
                $user_ext_result[0]['degree_school'] = '';
                $user_ext_result[0]['degree_major'] = '';
            }
            return $user_ext_result;
        }else{
            return 0;
        }
    }

    //获取乙方个人页面甲方信息
    public function getFetcherJFInfo($user_id){
        $user_ext_field = array('agent_j','agent_j_desc','user_id');
        $user_ext_map['user_id'] = array('eq',$user_id);
        $user_ext_result = $this->field($user_ext_field)->where($user_ext_map)->select();
        if(is_array($user_ext_result)){
            foreach($user_ext_result as &$user_ext_result_one){
                $mission_field = array('mission_name','jobs','salary2','location2','duration','status');
                $mission_order = '`status` desc,`publish_time` desc';
                $mission_map['publisher_id'] = array('eq',$user_ext_result_one['user_id']);
                $mission_result = $this->table('Mission')->field($mission_field)->order($mission_order)->where($mission_map)->select();
                if(is_array($mission_result)){
                    $user_ext_result_one['mission'] = $mission_result;
                }else{
                    //获取mission表信息失败
                }
            }
            return $user_ext_result[0];
        }else{
            return 0;
        }
    }

} 